package com.winskysoft.frame;

import java.sql.SQLException;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.List;

import com.winskysoft.entity.Table;
import com.winskysoft.tool.DatabaseTool;

public class TablePanel extends Composite implements SelectionListener {
	private Text text;
	private List leftList;
	private List rightList;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public TablePanel(Composite parent, int style) {
		super(parent, style);

		try {
			DatabaseTool.load();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Table[] table = DatabaseTool.getList();

		String[] str = new String[table.length];
		for (int i = 0; i < str.length; i++) {
			str[i] = table[i].getName();
		}

		leftList = new List(this, SWT.BORDER | SWT.MULTI);
		leftList.setFont(SWTResourceManager.getFont("宋体", 12, SWT.NORMAL));
		leftList.setItems(str);
		leftList.setBounds(22, 40, 210, 254);

		rightList = new List(this, SWT.BORDER | SWT.MULTI);
		rightList.setItems(new String[] {});
		rightList.setFont(SWTResourceManager.getFont("宋体", 12, SWT.NORMAL));
		rightList.setBounds(316, 40, 223, 254);

		Button btnRight = new Button(this, SWT.NONE);
		btnRight.addSelectionListener(new BtnRightSelectionListener());
		btnRight.setBounds(238, 50, 72, 22);
		btnRight.setText("->");

		Button btnRightAll = new Button(this, SWT.NONE);
		btnRightAll.addSelectionListener(new BtnRightAllSelectionListener());
		btnRightAll.setText("---->");
		btnRightAll.setBounds(238, 108, 72, 22);

		Button btnLeftAll = new Button(this, SWT.NONE);
		btnLeftAll.addSelectionListener(new BtnLeftAllSelectionListener());
		btnLeftAll.setText("<----");
		btnLeftAll.setBounds(238, 224, 72, 22);

		Button btnLeft = new Button(this, SWT.NONE);
		btnLeft.addSelectionListener(new BtnLeftSelectionListener());
		btnLeft.setText("<-");
		btnLeft.setBounds(238, 272, 72, 22);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(this);
		btnNewButton.setBounds(383, 312, 115, 30);
		btnNewButton.setText("\u4E0B\u4E00\u6B65");

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		CheckPanel tablePanel = new CheckPanel(getParent(), SWT.NONE);
		tablePanel.setSize(getSize());
		DatabaseTool.setSelect(rightList.getItems());
		this.dispose();
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub

	}

	private class BtnRightAllSelectionListener extends SelectionAdapter {
		@Override
		public void widgetSelected(SelectionEvent e) {
			rightList.setItems(leftList.getItems());

		}
	}

	private class BtnLeftAllSelectionListener extends SelectionAdapter {
		@Override
		public void widgetSelected(SelectionEvent e) {
			rightList.setItems(new String[0]);
		}
	}

	private class BtnRightSelectionListener extends SelectionAdapter {
		@Override
		public void widgetSelected(SelectionEvent e) {
			String[] ta = rightList.getItems();
			String[] selecs = leftList.getSelection();

			for (int j = 0; j < selecs.length; j++) {
				boolean flg = false;
				for (int i = 0; i < ta.length; i++) {
					if (ta[i].equals(selecs[j])) {
						flg = true;
						break;
					}
				}
				if (!flg) {
					rightList.add(selecs[j]);
				}
			}

		}
	}

	private class BtnLeftSelectionListener extends SelectionAdapter {
		@Override
		public void widgetSelected(SelectionEvent e) {
			String[] selecs = rightList.getSelection();

			for (int i = 0; i < selecs.length; i++) {
				rightList.remove(selecs[i]);
			}

		}
	}
}
